/**
 * drawing数据查询条件规则函数
 * @param {*} DrawingModel
 * @param {*} searchOption
 * @returns
 */
const { Op } = require('sequelize');

async function fetchData(DrawingModel, searchOption) {
	const { startDate, endDate, limit } = searchOption;

	return DrawingModel.find({
		['drawing.floor.list']: {
			$size: 1
		},
		['drawing.floor.list.roomList']: {
			$elemMatch: {
				type: { $regex: /厨房/g }  // 不区分大小写
			}
		},
		['drawing.floor.list.stuffList']: {
			$not: { $size: 0 }
		},
		createdAt: {
			$gte: startDate, // 大于等于开始日期
			$lte: endDate    // 小于等于结束日期
		}
	})
		.limit(limit);
}

async function fetchMysqlFloorplanData(Model, searchOption) {
	const { fpidList } = searchOption;

	return await Model.findAll({
		where: {
			fpid: { [Op.in]: fpidList }
		}
	});
}

async function fetchMysqlFloorplanImageData(Model, options = {
	type: 'COLOR',
	level: 0,
	fpidList: []
}) {
	return await Model.findAll({
		where: {
			type: options.type,
			level: options.level,
			fpid: { [Op.in]: options.fpidList }
		}
	});
}

module.exports = {
	fetchData,
	fetchMysqlFloorplanData,
	fetchMysqlFloorplanImageData
};